package com.ordobill.webapp.action;

import java.util.ArrayList;

import javax.servlet.http.*;

import org.apache.struts.action.*;
import org.apache.commons.logging.*;
import com.ibatis.sqlmap.client.*;
import com.ordobill.webapp.beans.Member;
import com.ordobill.webapp.beans.Project;
import com.ordobill.webapp.common.SqlMapClientManager;
import org.apache.struts.actions.*;

public class MemberAction extends DispatchAction{
	
	@SuppressWarnings("unused")
    private Log log = LogFactory.getFactory().getInstance(this.getClass().getName());
	private SqlMapClient sqlMap = SqlMapClientManager.getSqlMapClient();

	@SuppressWarnings("unchecked")
    public ActionForward list(ActionMapping mapping,
			                   ActionForm form,
			                   HttpServletRequest request,
			                   HttpServletResponse response) throws Exception {
		
		Member memberForm = (Member)form; 
		ArrayList<Member> memberList = (ArrayList<Member>) sqlMap.queryForList("Member.list", memberForm);
		int totalCount = (Integer) sqlMap.queryForObject("Member.listCount", memberForm);
		
		request.setAttribute("memberList", memberList);
		request.setAttribute("totalCount", totalCount);
		request.setAttribute("memberForm", memberForm);
		return mapping.findForward("list");
	}
	
	public ActionForward info(ActionMapping mapping,
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
		Member memberForm = (Member)form;
		Member memberData = (Member)sqlMap.queryForObject("Member.list", memberForm);
		
		request.setAttribute("memberData", memberData);

        return mapping.findForward("info");
    }
	
	@SuppressWarnings("unchecked")
	public ActionForward insert(ActionMapping mapping,
	        ActionForm form,
	        HttpServletRequest request,
	        HttpServletResponse response) throws Exception {
	    Member memberForm = (Member)form;
	    ArrayList<Member> memList = (ArrayList<Member>)sqlMap.queryForList("Member.list", memberForm);
	    ActionMessages msg = new ActionMessages();
	    
	    if(memList.size() > 0){
	        msg.add("message", new ActionMessage("이미 등록된 아이디 입니다."));
	        saveMessages(request, msg);
	        return mapping.getInputForward();
	    }
	    int memUid = (Integer) sqlMap.insert("Member.memberInsert", memberForm);
	    if(memUid == 0){
	        msg.add("message", new ActionMessage("저장에 실패했습니다."));
	    }else if(memUid > 0){
	        msg.add("message", new ActionMessage("저장에 성공했습니다."));
	    }
	    
	    saveMessages(request, msg);
	    
	    return mapping.findForward("success");
	}
	
	public ActionForward update(ActionMapping mapping,
                                ActionForm form,
                                HttpServletRequest request,
                                HttpServletResponse response) throws Exception {
		Member memberForm = (Member)form;
		ActionMessages msg = new ActionMessages();
		int message = -1;
		    message = (Integer)sqlMap.update("Member.memberUpdate", memberForm);
		       
	    if(message == 0){
	        msg.add("message", new ActionMessage("저장에 실패했습니다."));
	    }else if(message > 0){
	        msg.add("message", new ActionMessage("저장에 성공했습니다."));
	    }
	        
		Member memberData = (Member)sqlMap.queryForObject("Member.list", memberForm);
		saveMessages(request, msg);
		request.setAttribute("memberData", memberData);
        return mapping.findForward("update");
    }
	
	
	@SuppressWarnings("unchecked")
    public ActionForward delete(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
    	
		Member memberForm = (Member)form;
		ActionMessages msg = new ActionMessages();
		int message = -1;
		    message = (Integer) sqlMap.delete("Member.memberDelete", memberForm);
		       System.out.println("message====:"+message);
	       if(message == 0){
		        msg.add("message", new ActionMessage("삭제에 실패했습니다."));
		    }else if(message > 0){
		        msg.add("message", new ActionMessage("삭제에 성공했습니다."));
		    }
	       saveMessages(request, msg);
           request.setAttribute("memberData", memberForm);
		return mapping.findForward("delete");
    }
}
